package repository

import (
	dbspo2 "energy-amiot-platform/model/dbspo"

	"gorm.io/gorm"
)

var (
	OBJECTMODEL modelRepo
)

type modelRepo struct {
}

func (p *modelRepo) Exist(db *gorm.DB, key uint64) (*gorm.DB, bool) {
	var count int64
	tx := db.Model(&dbspo2.Product{}).Where("id=?", key).Count(&count)
	return tx, count > 0
}

func (p *modelRepo) ExistWithName(db *gorm.DB, name string) (*gorm.DB, bool) {
	var count int64
	tx := db.Model(&dbspo2.Product{}).Where("name=?", name).Count(&count)
	return tx, count > 0
}

func (p *modelRepo) ExistByProductIdAndName(db *gorm.DB, pid uint64, name string) (*gorm.DB, bool) {
	var count int64
	return db.Model(&dbspo2.Model{}).Where("pid=? and name=?", pid, name).Count(&count), count > 0
}

func (p *modelRepo) AddOne(db *gorm.DB, model *dbspo2.Model) *gorm.DB {
	return db.Create(model)
}
